﻿function Get-Tool-Path-From-NuGet-Package  {
	param (
		$baseDirectory,
        $toolexecutablename
    )
	$packagesDir = Join-Path $baseDirectory "src\Packages"
	$exePath = dir $packagesDir -recurse | where { $_.PSIsContainer -eq $false -and $_.Name -eq $toolexecutablename } | foreach { $_.FullName } | select -first 1
	
	return $exePath
}	

function Get-File-Exists-On-Path {
	param (
		[string]$file
	)
	$results = ($Env:Path).Split(";") | Get-ChildItem -filter $file -erroraction silentlycontinue
	$found = ($results -ne $null)
	return $found
}

function Get-Hg-Id {
	$id = "0000000"    
	if (Get-File-Exists-On-Path "hg.exe") {
		$id = hg identify -i -r .
	}
	else {
		if ("$env:BUILD_VCS_NUMBER".length -gt 0) {
			$id = "$env:BUILD_VCS_NUMBER"
		}
	}
	return $id
}

function Generate-Assembly-Info {
	param(
		[string]$clsCompliant = "true",
		[string]$title,
		[string]$description,
		[string]$company,
		[string]$product,
		[string]$copyright,
		[string]$version,
		[string]$fileVersion,
		[string]$informationalVersion,
		[string]$file = $(throw "file is a required parameter.")
	)   
	
	$hgId = Get-Hg-Id
	
	$asmInfo = "using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: SuppressIldasmAttribute()]
[assembly: CLSCompliantAttribute($clsCompliant )]
[assembly: ComVisibleAttribute(false)]
[assembly: AssemblyTitleAttribute(""$title"")]
[assembly: AssemblyDescriptionAttribute(""$description"")]
[assembly: AssemblyCompanyAttribute(""$company"")]
[assembly: AssemblyProductAttribute(""$product"")]
[assembly: AssemblyCopyrightAttribute(""$copyright"")]
[assembly: AssemblyVersionAttribute(""$version"")]
[assembly: AssemblyInformationalVersionAttribute(""$informationalVersion"")]
[assembly: AssemblyFileVersionAttribute(""$fileVersion"")]
[assembly: AssemblyDelaySignAttribute(false)]
"

	$dir = [System.IO.Path]::GetDirectoryName($file)
	if ([System.IO.Directory]::Exists($dir) -eq $false) {
		Write-Host "Creating directory $dir"
		[System.IO.Directory]::CreateDirectory($dir)
	}
	Write-Host "Generating assembly info file: $file"
	Write-Output $asmInfo > $file
}